﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;

namespace DAL
{
    /// <summary>
    /// 邮箱信息
    /// </summary>
    public class EmailInfo
    {
        public EmailInfo()
        { }

        /// <summary>
        /// 添加邮箱信息
        /// </summary>
        /// <param name="model">信息实体</param>
        /// <returns>是否执行成功</returns>
        public bool EmailAdd(MODEL.EmailInfo model)
        {
            bool flag = false;

            SqlParameter[] parameter ={
                                         new SqlParameter("@name",SqlDbType.VarChar,50),
                                         new SqlParameter("@groundid",SqlDbType.Int),
                                         new SqlParameter("@email",SqlDbType.VarChar,100),
                                         new SqlParameter("@createid",SqlDbType.VarChar,20)
                                     };
            parameter[0].Value = model.EMAILNAME;
            parameter[1].Value = model.EGROUNDID;
            parameter[2].Value = model.EMAILADDRESS;
            parameter[3].Value = model.ECREATEID;
            try
            {
                flag = DBUtility.DbHelper.RunProcedure("ProEmailAdd", parameter);
            }
            catch (Exception)
            { flag = false; }

            return flag;
        }
        /// <summary>
        /// 删除邮箱信息
        /// </summary>
        /// <param name="id">编号</param>
        /// <returns>是否执行成功</returns>
        public bool EmailDelete(int id)
        {
            bool flag = false;
            SqlParameter[] parameter={
                                         new SqlParameter("@id",SqlDbType.Int)
                                     };
            parameter[0].Value = id;
            try
            {
                flag = DBUtility.DbHelper.RunProcedure("ProEmailDelete", parameter);
            }
            catch (Exception)
            { flag = false; }
            return flag;
        }
        /// <summary>
        /// 修改邮箱信息
        /// </summary>
        /// <param name="model">信息实体</param>
        /// <returns>是否执行成功</returns>
        public bool EmailUpdate(MODEL.EmailInfo model)
        {
            bool flag = false;
            SqlParameter[] parameter ={
                                         new SqlParameter("@id",SqlDbType.Int),
                                         new SqlParameter("@name",SqlDbType.VarChar,50),
                                         new SqlParameter("@groundid",SqlDbType.Int),
                                         new SqlParameter("@email",SqlDbType.VarChar,100)
                                     };
            parameter[0].Value = model.ID;
            parameter[1].Value = model.EMAILNAME;
            parameter[2].Value = model.EGROUNDID;
            parameter[3].Value = model.EMAILADDRESS;
            try
            {
                flag = DBUtility.DbHelper.RunProcedure("ProEmailUpdate", parameter);
            }
            catch (Exception)
            { flag = false; }
            return flag;
        }
        /// <summary>
        /// 邮箱信息列表
        /// </summary>
        /// <returns>列表</returns>
        public DataSet EmailList()
        {
            return DBUtility.DbHelper.RunProcedure("ProEmailSelect");
        }
        /// <summary>
        /// 邮箱信息列表
        /// </summary>
        /// <param name="groupId">分组编号</param>
        /// <returns>列表</returns>
        public DataSet EmailList(int groupId)
        {
            DataSet ds = new DataSet();
            SqlParameter[] parameter ={
                                         new SqlParameter("@id",SqlDbType.Int)
                                     };
            parameter[0].Value = groupId;
            try
            {
                ds = DBUtility.DbHelper.RunProcedureS("ProEmailSelectByGroup", parameter);
            }
            catch (Exception)
            { ds = null; }
            return ds;
        }
    }
    /// <summary>
    /// 邮箱分组
    /// </summary>
    public class EmailGroup
    {
        public EmailGroup()
        { }
        /// <summary>
        /// 添加分组信息
        /// </summary>
        /// <param name="model">信息实体</param>
        /// <returns>是否执行成功</returns>
        public bool EmailGroupAdd(MODEL.EmailGroup model)
        {
            bool flag = false;
            SqlParameter[] parameter ={
                                         new SqlParameter("@name",SqlDbType.VarChar,20),
                                         new SqlParameter("@createid",SqlDbType.VarChar,20),
                                         new SqlParameter("@Return",SqlDbType.Int)
                                     };
            parameter[0].Value = model.EGROUPNAME;
            parameter[1].Value = model.ECREATEID;
            parameter[2].Direction = ParameterDirection.ReturnValue;
            
            try
            {
                flag = DBUtility.DbHelper.RunProcedure("ProEmailgroupAdd", parameter);
            }
            catch (Exception)
            { flag = false; }
            return flag;
        }
        /// <summary>
        /// 删除分组信息
        /// </summary>
        /// <param name="id">编号</param>
        /// <returns>是否执行成功</returns>
        public bool EmailGroupDelete(int id)
        {
            bool flag = false;
            SqlParameter[] parameter ={
                                         new SqlParameter("@id",SqlDbType.Int),
                                         new SqlParameter("@Return",SqlDbType.Int)
                                     };
            parameter[0].Value = id;
            parameter[1].Direction = ParameterDirection.ReturnValue;
            try
            {
                flag = DBUtility.DbHelper.RunProcedure("ProEmailgroupDelete", parameter);
            }
            catch (Exception)
            { flag = false; }
            return flag;
        }
        /// <summary>
        /// 修改分组信息
        /// </summary>
        /// <param name="model">信息实体</param>
        /// <returns>是否执行成功</returns>
        public bool EmailGroupUpdate(MODEL.EmailGroup model)
        {
            bool flag = false;
            SqlParameter[] parameter ={
                                         new SqlParameter("@id",SqlDbType.Int),
                                         new SqlParameter("@name",SqlDbType.VarChar,20),
                                         new SqlParameter("@Return",SqlDbType.Int)
                                     };
            parameter[0].Value = model.ID;
            parameter[1].Value = model.EGROUPNAME;
            parameter[2].Direction = ParameterDirection.ReturnValue;
            try
            {
                flag = DBUtility.DbHelper.RunProcedure("ProEmailgroupUpdate", parameter);
            }
            catch (Exception)
            { flag = false; }
            return flag;
        }
        /// <summary>
        /// 分组信息列表
        /// </summary>
        /// <returns>列表</returns>
        public DataSet EmailGroupList()
        {
            return DBUtility.DbHelper.RunProcedure("ProEmailgroupSelect");
        }
        /// <summary>
        /// 分组信息列表
        /// </summary>
        /// <param name="id">编号</param>
        /// <returns>列表</returns>
        public DataSet EmailGroupList(int id)
        {
            DataSet ds = new DataSet();
            SqlParameter[] parameter ={
                                         new SqlParameter("@id",SqlDbType.Int)
                                     };
            parameter[0].Value = id;

            try
            {
                ds = DBUtility.DbHelper.RunProcedureS("ProEmailgroupSelectByid", parameter);
            }
            catch (Exception)
            { ds = null; }
            return ds;
        }
        /// <summary>
        /// 分组信息列表
        /// </summary>
        /// <param name="name">名称</param>
        /// <returns>列表</returns>
        public DataSet EmailGroupList(string name)
        {
            DataSet ds = new DataSet();
            SqlParameter[] parameter ={
                                         new SqlParameter("@name",SqlDbType.VarChar,20)
                                     };

            try
            {
                ds = DBUtility.DbHelper.RunProcedureS("ProEmailgroupSelectByName", parameter);
            }
            catch (Exception)
            { ds = null; }
            return ds;
        }
    }
    /// <summary>
    /// 发送日志
    /// </summary>
    public class EmailLog
    {
        public EmailLog()
        { }
        /// <summary>
        /// 添加发送列表
        /// </summary>
        /// <param name="model">信息实体</param>
        /// <returns>是否执行成功</returns>
        public bool EmailLogAdd(MODEL.EmailLog model)
        {
            bool flag = false;
            SqlParameter[] parameter ={
                                         new SqlParameter("@toemail",SqlDbType.VarChar,100),
                                         new SqlParameter("@fromemail",SqlDbType.VarChar,100),
                                         new SqlParameter("@describe",SqlDbType.VarChar,20),
                                         new SqlParameter("@createid",SqlDbType.VarChar,20),
                                         new SqlParameter("@content",SqlDbType.Text)
                                     };
            parameter[0].Value = model.ETOMAILADDRESS;
            parameter[1].Value = model.EFROMMAILADDRESS;
            parameter[2].Value = model.EDESCRIBE;
            parameter[3].Value = model.ECREATEID;
            parameter[4].Value = model.ECONTENT;

            try
            {
                flag = DBUtility.DbHelper.RunProcedure("ProEmaillogAdd", parameter);
            }
            catch (Exception)
            { flag = false; }
            return flag;
        }
        /// <summary>
        /// 删除发送列表
        /// </summary>
        /// <param name="id">编号</param>
        /// <returns>是否执行成功</returns>
        public bool EmailLogDelete(int id)
        {
            bool flag = false;
            SqlParameter[] parameter ={
                                         new SqlParameter("@id",SqlDbType.Int)
                                     };
            parameter[0].Value = id;

            try
            {
                flag = DBUtility.DbHelper.RunProcedure("ProEmaillogDelete", parameter);
            }
            catch (Exception)
            { flag = false; }
            return flag;
        }
        /// <summary>
        /// 发送列表
        /// </summary>
        /// <param name="id">编号</param>
        /// <returns>列表</returns>
        public DataSet EmailLogList(string id)
        {
            DataSet ds = new DataSet();
            SqlParameter[] parameter ={
                                         new SqlParameter("@id",SqlDbType.VarChar,20)
                                     };
            parameter[0].Value = id;
            try
            {
                ds = DBUtility.DbHelper.RunProcedureS("ProEmaillogSelectByCreateId", parameter);
            }
            catch (Exception)
            { ds = null; }
            return ds;
        }
        /// <summary>
        /// 发送列表
        /// </summary>
        /// <param name="state">状态</param>
        /// <returns>列表</returns>
        public DataSet EmailLogList(int state)
        {
            DataSet ds = new DataSet();
            SqlParameter[] parameter ={
                                         new SqlParameter("@state",SqlDbType.Int)
                                     };
            parameter[0].Value = state;
            try
            {
                ds = DBUtility.DbHelper.RunProcedureS("ProEmaillogSelectByState", parameter);
            }
            catch (Exception)
            { ds = null; }
            return ds;
        }

    }
    /// <summary>
    /// SMTP设置
    /// </summary>
    public class EmailSmtp
    {
        public EmailSmtp()
        { }
        /// <summary>
        /// 添加smtp信息
        /// </summary>
        /// <param name="model">信息实体</param>
        /// <returns>是否执行成功</returns>
        public bool EmailSmtpAdd(MODEL.EmailSmtp model)
        {
            bool flag = false;
            SqlParameter[] parameter ={
                                         new SqlParameter("@username",SqlDbType.VarChar,100),
                                         new SqlParameter("@password",SqlDbType.VarChar,100),
                                         new SqlParameter("@address",SqlDbType.VarChar,100),
                                         new SqlParameter("@port",SqlDbType.Int),
                                         new SqlParameter("@createid",SqlDbType.VarChar,20),
                                         new SqlParameter("@Return",SqlDbType.Int)
                                     };
            parameter[0].Value = model.EUSERNAME;
            parameter[1].Value = model.EPASSWORD;
            parameter[2].Value = model.ESMTPADDRESS;
            parameter[3].Value = model.EPORT;
            parameter[4].Value = model.ECREATEID;
            parameter[5].Direction = ParameterDirection.ReturnValue;
            try
            {
                flag = DBUtility.DbHelper.RunProcedure("ProEmailsmtpAdd", parameter);
            }
            catch (Exception)
            { flag = false; }
            return flag;
        }
        /// <summary>
        /// 删除smtp信息
        /// </summary>
        /// <param name="id">编号</param>
        /// <returns>是否执行成功</returns>
        public bool EmailSmtpDelete(int id)
        {
            bool flag = false;
            SqlParameter[] parameter ={
                                         new SqlParameter("@id",SqlDbType.Int)
                                     };
            parameter[0].Value = id;
            try
            {
                flag = DBUtility.DbHelper.RunProcedure("ProEmailsmtpDelete", parameter);
            }
            catch (Exception)
            { flag = false; }
            return flag;
        }
        /// <summary>
        /// 修改smtp信息
        /// </summary>
        /// <param name="model">信息实体</param>
        /// <returns>是否执行成功</returns>
        public bool EmailSmtpUpdate(MODEL.EmailSmtp model)
        {
            bool flag = false;
            SqlParameter[] parameter ={
                                         new SqlParameter("@id",SqlDbType.Int),
                                         new SqlParameter("@username",SqlDbType.VarChar,100),
                                         new SqlParameter("@password",SqlDbType.VarChar,100),
                                         new SqlParameter("@address",SqlDbType.VarChar,100),
                                         new SqlParameter("@port",SqlDbType.Int),
                                         new SqlParameter("@Return",SqlDbType.Int)
                                     };
            parameter[0].Value = model.ID;
            parameter[1].Value = model.EUSERNAME;
            parameter[2].Value = model.EPASSWORD;
            parameter[3].Value = model.ESMTPADDRESS;
            parameter[4].Value = model.EPORT;
            parameter[5].Direction = ParameterDirection.ReturnValue;
            try
            {
                flag = DBUtility.DbHelper.RunProcedure("ProEmailsmtpUpdate", parameter);
            }
            catch (Exception)
            { flag = false; }
            return flag;
        }
        /// <summary>
        /// smtp列表
        /// </summary>
        /// <returns>列表</returns>
        public DataSet EmailSmtpList()
        {
            return DBUtility.DbHelper.RunProcedure("ProEmailsmtpSelect");
        }
        /// <summary>
        /// smtp列表
        /// </summary>
        /// <param name="id">编号</param>
        /// <returns>列表</returns>
        public DataSet EmailSmtpList(int id)
        {
            DataSet ds = new DataSet();
            SqlParameter[] parameter ={
                                         new SqlParameter("@id",SqlDbType.Int)
                                     };
            parameter[0].Value = id;
            try
            {
                ds = DBUtility.DbHelper.RunProcedureS("ProEmailsmtpSelectById", parameter);
            }
            catch (Exception)
            { ds = null; }
            return ds;
        }
    }
}
